import axios from './config'
import md5 from 'js-md5'
import { Base64 } from 'js-base64';
import { PcCookie, enums } from '../utils'
import { Toast } from 'vant';
import qs from 'qs';
//后台数据返回成功标识
export const ERR_OK = '000';
// TOKEN过期
export const TOKEN_EXPIRE = '320';

//页面接口通用接口 Post
export const ajaxPost = async function (url = '', params = {}, option = {}) {
  if (option.contentType) axios.defaults.headers['Content-Type'] = option.contentType;
  else axios.defaults.headers['Content-Type'] = 'application/json';
  try {
    Toast.loading({
      duration: 20000
    });
    let result = await axios.post(url, params, option);
    return new Promise((resolve, reject) => {
      Toast.clear();
      if(result.data.code === 0) {
        Toast(result.data.msg)
      }
      resolve(result.data);
    })
  } catch (err) {
    Toast.clear();
  }
}

//页面接口通用接口 Get
export const ajaxGet = async function (url = '', params = {}, option = {}) {
  try {
    Toast.loading({
      duration: 20000
    });
    //  console.log('params||'+JSON.stringify(params))
    let result = await axios.get(url, {params}, option);
    return new Promise((resolve, reject) => {
      Toast.clear();
      if(result.data.code === 0) {
        Toast(result.data.msg)
      }
      resolve(result.data);
    })
  } catch (err) {
    Toast.clear()
    console.log({err});
  }
}

// 首页轮播图
export async function getSlideImg(data, option) {
  return await ajaxGet('/api/shop/slide_img', data, option)
}
// 首页商家类别
export async function getShopCate(data, option) {
  return await ajaxGet('/api/shop/shop_cate', data, option)
}
// 首页商家列表
export async function postShopList(data, option) {
  // data = {"lat":22.720968,"lng":114.246899,"order":""};
  return await ajaxPost('/api/shop/shop_list', data, option)
}
// 图片上传接口
export async function postUpload(data, option) {
  return await ajaxPost('/api/shop/upload', data, option)
}
// 返回门店分类
export async function getCate(data, option) {
  return await ajaxPost('/api/shop/get_cate', data, option)
}
// 门店上传
export async function postAddStore(data, option) {
  return await ajaxPost('/api/shop/add_store', data, option)
}
// 上传商品
export async function postAddGoods(data, option) {
  return await ajaxPost('/api/shop/add_goods', data, option)
}
// 首页推荐商家
export async function postRecoShop(data, option) {
  return await ajaxPost('/api/shop/reco_shop', data, option)
}

// 商品详情
export async function postGoodsDetail(data, option) {
    return await ajaxPost('/api/shop/goods_detail', data, option)
}

// 收藏商品
export async function postAddLikeGoods(data, option) {
    return await ajaxPost('/api/shop/collect_goods', data, option)
}

//收藏店铺
export async function postAddLikeShop(data, option) {
    return await ajaxPost('/api/shop/collect_shop', data, option)
}

//我收藏的商品
export async function postMyCollection(data, option) {
    return await ajaxPost('/api/shop/my_collect', data, option)
}

//我收藏的商品
export async function postPayFor(data, option) {
    return await ajaxPost('/api/shop/pay', data, option)
}

//我的订单
export async function postMyOrder(data, option) {
    return await ajaxPost('/api/shop/my_order', data, option)
}

//商家订单
export async function postStoreOrder(data, option) {
    return await ajaxPost('/api/shop/shop_order', data, option)
}

//门店首页
export async function postShop(data, option) {
    return await ajaxPost('/api/shop/shop', data, option)
}

//城市列表
export async function getCity(data, option) {
    return await ajaxGet('/api/shop/get_city', data, option)
}


//门店信息（用于修改信息）
export async function postGetStoreinfo(data, option) {
  return await ajaxPost('/api/shop/get_storeinfo', data, option)
}

//修改店铺
export async function postEditStore(data, option) {
  return await ajaxPost('/api/shop/edit_store', data, option)
}

//修改商品
export async function postEditGoods(data, option) {
  return await ajaxPost('/api/shop/edit_goods', data, option)
}

//修改商品
export async function postGetGoodsInfo(data, option) {
  return await ajaxPost('/api/shop/get_goods_info', data, option)
}

// 我商店的商品
export async function postgetGoodsList(data, option) {
  return await ajaxPost('/api/shop/get_goods_list', data, option)
}

// 个人信息
export async function postGetInfo(data, option) {
  return await ajaxPost('/api/shop/get_info', data, option)
}

// 我的收货地址
export async function addressList(data, option) {
  return await ajaxPost('/api/shop/address_list', data, option)
}

// 我的收货地址
export async function sendGoods(data, option) {
  return await ajaxPost('/api/shop/send_goods', data, option)
}

// 商品状态变更
export async function changeGoodsStatus(data, option) {
  return await ajaxPost('/api/shop/goods_status', data, option)
}

// 获取微信配置
export async function getWxcfg(data, option) {
  return await ajaxPost('/api/shop/get_wxcfg', data, option)
}

// 用户协议
export async function getContract(data, option) {
  return await ajaxPost('/api/shop/get_contract', data, option)
}
// 发布推广
export function add_adv(data) {
  return ajaxPost('/api/shop/add_adv', data)
}

// 发布推广
export function adv_detail(data) {
  return ajaxPost('/api/shop/adv_detail', data)
}

// 评论回复
export function add_adv_chat(data) {
  return ajaxPost('/api/shop/add_adv_chat', data)
}

// 推广列表
export function adv_list(data) {
  return ajaxPost('/api/shop/adv_list', data)
}

// 修改推广
export function edit_adv(data) {
  return ajaxPost('/api/shop/edit_adv', data)
}

// 点赞接口
export function adv_point(data) {
  return ajaxPost('/api/shop/adv_point', data)
}

// 点赞接口
export function my_adv(data) {
  return ajaxPost('/api/shop/my_adv', data)
}
